//
//  ChosePayView.m
//  fitnessmanage
//
//  Created by wujian on 2017/12/13.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "ChosePayView.h"

@implementation ChosePayInfoView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        UIImageView *typeImageView = [[UIImageView alloc] init];
        [self addSubview:typeImageView];
        self.typeImageView = typeImageView;
        
        UILabel *titleTextLabel = [[UILabel alloc] init];
        titleTextLabel.font = MidBigFont;
        titleTextLabel.textColor = CSColorFromRGB(0x333333);
        [self addSubview:titleTextLabel];
        self.titleTextLabel = titleTextLabel;
        
        UIButton *choseBtn = [[UIButton alloc] init];
        [choseBtn setImage:@"icon_radio"];
        [choseBtn setSelectedImage:@"icon_radioch"];
        [self addSubview:choseBtn];
        self.choseBtn = choseBtn;
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{

    [self.typeImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self).offset(15);
        
        make.left.mas_equalTo(self).offset(15);
        
        make.bottom.mas_equalTo(self).offset(-15);
    }];
    
    [self.titleTextLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self);
        
        make.left.mas_equalTo(self.typeImageView.mas_right).offset(10);
    }];
    
    [self.choseBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self);
        
        make.right.mas_equalTo(self).offset(-15);
    }];
}

@end


@interface ChosePayView()

@property(nonatomic,strong)NSString *selectStr;

@property(nonatomic,strong)NSString *selectStrId;

@property(nonatomic,strong)NSMutableDictionary *imageDict;

@property(nonatomic,strong)NSMutableArray *viewArray;

@end

@implementation ChosePayView

-(id)initWithFrame:(CGRect)frame{
    
    CGRect initialFrame;
    
    if (CGRectIsEmpty(frame)) {
        
        initialFrame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
    } else {
        
        initialFrame = frame;
    }
    
    self = [super initWithFrame:initialFrame];
    
    if (self) {
        
        self.imageDict = [[NSMutableDictionary alloc] init];
        
        self.viewArray = [[NSMutableArray alloc] init];
        
        UIView *alphaView = [[UIView alloc] init];
        alphaView.backgroundColor = [CSBlackColor colorWithAlphaComponent:0.4];
        [self addSubview:alphaView];
        self.alphaView = alphaView;
        
        UIView *whiteView = [[UIView alloc] init];
        whiteView.backgroundColor = CSWhiteColor;
        [self addSubview:whiteView];
        self.whiteView = whiteView;
        
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"选择支付方式";
        titleLabel.font = TitleBoldFont;
        [whiteView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        UIButton *closeBtn = [[UIButton alloc] init];
        [closeBtn setImage:@"pop_icon_close"];
        [closeBtn addTarget:self action:@selector(hiddenWithAnimation)];
        [whiteView addSubview:closeBtn];
        self.closeBtn = closeBtn;
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = LineBackGroundColor;
        [whiteView addSubview:lineView];
        self.lineView = lineView;
        
        /**
         线上
         */
        UIView *onLineView = [[UIView alloc] init];
        onLineView.backgroundColor = ButtonBackGroundColor;
        [whiteView addSubview:onLineView];
        self.onLineView = onLineView;
        
        UILabel *onTitleLabel = [[UILabel alloc] init];
        onTitleLabel.font = BoldMidFont;
        onTitleLabel.text = @"在线支付";
        [whiteView addSubview:onTitleLabel];
        self.onTitleLabel = onTitleLabel;
        
        [self.imageDict setObject:@"icon_pay_wechat" forKey:@"微信扫码支付"];
        [self.imageDict setObject:@"icon_pay_wechat" forKey:@"微信支付"];
        [self.imageDict setObject:@"icon_pay_alipay" forKey:@"支付宝"];
        [self.imageDict setObject:@"icon_pay_alipay" forKey:@"支付宝扫码"];
        [self.imageDict setObject:@"icon_pay_cash" forKey:@"现金支付"];
        [self.imageDict setObject:@"icon_pay_card" forKey:@"刷卡支付"];
        [self.imageDict setObject:@"icon_pay_accounts" forKey:@"转账支付"];
        [self.imageDict setObject:@"icon_pay_other" forKey:@"其他支付"];
        
        
        MOFSBottomView *bottomView = [[MOFSBottomView alloc] init];
        [whiteView addSubview:bottomView];
        self.bottomView = bottomView;
        
        __weak __typeof(self) weakSelf = self;
        
        self.bottomView.cancelBlock = ^ {
            
            [weakSelf hiddenWithAnimation];
        };
        
        self.bottomView.commitBlock = ^ {
            
            if (!weakSelf.selectStr) {
                
                [MBProgressHUD showText:@"请选择支付方式" toview:self];
                
                return ;
            }
            
            if (weakSelf.commitBlock) {
                
                weakSelf.commitBlock(weakSelf.selectStr,weakSelf.selectStrId);
                
                [weakSelf hiddenWithAnimation];
            }
        };
        
        [self loadData];
        //[self setConstraints];
    }
    
    return self;
}

-(void)loadData{
    
    //GET_PayWaysListData
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
//    VenueModel *venue = [VenueModel venue];
//
//    [params setObject:venue.venueId forKey:@"ShopID"];
//
//    [params setObject:[NSString stringWithFormat:@"%d",1] forKey:@"page"];
//
//    [params setObject:[NSString stringWithFormat:@"%d",100] forKey:@"rows"];
//
//    [HTTPTool GET:GET_PayWaysListData parameters:params
//
//          success:^(id responseObject){
//
//              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
//
//                  NSArray *rows = [NSArray arrayWithArray:[responseObject objectForKey:@"rows"]];
//
//                  for (NSDictionary *dict in rows) {
//
//                      NSString *name = [dict objectForKey:@"Name"];
//
//                      NSString *imageId = [dict objectForKey:@"ID"];
//
//                      ChosePayInfoView *tmpChatView = [[ChosePayInfoView alloc] init];;
//                      tmpChatView.titleTextLabel.text = name;
//                      tmpChatView.tag = [imageId integerValue];
//                      [tmpChatView addTarget:self action:@selector(btnClick:)];
//                      NSString *imageName = [self.imageDict objectForKey:name];
//                      if (imageName) {
//
//                          tmpChatView.typeImageView.image = [UIImage imageNamed:imageName];
//                      }
//                      else{
//
//                          tmpChatView.typeImageView.image = [UIImage imageNamed:@"icon_pay_other"];
//                      }
//                      [self.whiteView addSubview:tmpChatView];
//                      [self.viewArray addObject:tmpChatView];
//
//                      [self setConstraints];
//                  }
//              }
//
//          }
//
//          failure:^(NSError *error){
//
//          }];
}


-(void)setConstraints{
    
    [self.alphaView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
    }];
    
    [self.whiteView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.left.mas_equalTo(self).offset(20);
        make.right.mas_equalTo(self).offset(-20);
    }];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.whiteView).offset(20);
        make.centerX.mas_equalTo(self.whiteView);
    }];
    
    [self.closeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.whiteView).offset(-10);
        make.top.mas_equalTo(self.whiteView).offset(10);
    }];
    
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(30);
        make.left.width.mas_equalTo(self.whiteView);
        make.height.mas_equalTo(1);
    }];
    
    /**
     线上
     */
    [self.onLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(15);
        
        make.left.mas_equalTo(self.whiteView).offset(15);
        
        make.width.mas_equalTo(3);
        
        make.height.mas_equalTo(15);
    }];
    
    [self.onTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.onLineView);
        
        make.left.mas_equalTo(self.onLineView.mas_right).offset(5);
    }];
    
    
    ChosePayInfoView *tmpView;
    
    for (ChosePayInfoView *chatView in self.viewArray) {
        
        [chatView mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.left.mas_equalTo(self.whiteView);
            
            if (!tmpView) {
             
                make.top.mas_equalTo(self.onTitleLabel.mas_bottom).offset(20);
            }
            else{
                
                make.top.mas_equalTo(tmpView.mas_bottom);
                
            }
            make.right.mas_equalTo(self.whiteView).offset(-15);
        }];
        
        tmpView = chatView;
    }
    

    [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(tmpView.mas_bottom);

        make.left.width.bottom.mas_equalTo(self.whiteView);

        make.bottom.mas_equalTo(self.whiteView);
    }];
}

- (void)showWithAnimation {
    [self addViews];
    self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    CGFloat height = self.whiteView.frame.size.height;
    self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT + height / 2);
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT - height / 2);
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    }];
    
}

- (void)hiddenWithAnimation {
    CGFloat height = self.whiteView.frame.size.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT + height / 2);
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        [self hiddenViews];
    }];
}

- (void)addViews {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window addSubview:self];
}

- (void)hiddenViews {
    
    [self removeFromSuperview];
}


#pragma mark 按钮点击
-(void)btnClick:(ChosePayInfoView *)btn{
    
    
    for (ChosePayInfoView *chatView in self.viewArray) {
        
        if (chatView.tag == btn.tag) {
            
            chatView.choseBtn.selected = YES;
            
            self.selectStr = btn.titleTextLabel.text;
            
            self.selectStrId = [NSString stringWithFormat:@"%ld",(long)btn.tag];
        }
        else{
            
            chatView.choseBtn.selected = NO;
        }
    }

    
}
@end
